﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PlatWL.Data;
using System.Data;


namespace PlatWL.Services.Services
{
    public class BaseManage
    {

        #region 分类管理
        /// <summary>
        /// 查询分类
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="page"></param>
        /// <param name="Client"></param>
        /// <param name="topid"></param>
        /// <param name="InfoType"></param>
        public void Cate_List(ref Msg_Result msg, int Client, Msg_Page page, string infotype, int topid)
        {
            string strwhere = "InfoType='" + infotype + "' and TopID=" + topid;

            int total = 0;
            var dt = new pt_base_categoryb().GetDTByCommand("select * from pt_base_category where InfoType='" + infotype + "' and TopID=" + topid + " order by Sort ");
            msg.result = dt;
            msg.total = dt.Rows.Count;

        }


        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="Client"></param>
        /// <param name="adv"></param>
        public void Cate_Add(ref Msg_Result msg, int Client, pt_base_category adv)
        {
            bool result = false;
            if (adv.ID == 0)
            {
                adv.Client = Client;
                adv.CRTime = DateTime.Now;
                adv.Deleted = 0;
                adv.ISMsg = 0;
                adv.ISShow = 0;
                result = new pt_base_categoryb().Insert(adv);
            }
            else
            {
                result = new pt_base_categoryb().Update(adv);
            }
            if (!result)
            {
                msg.error = "更新失败";
            }
            else
            {
                msg.result = adv;
            }
        }

        /// <summary>
        /// 分类MODEL
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="Client"></param>
        /// <param name="ID"></param>
        public void Cate_Model(ref Msg_Result msg, int Client, int ID)
        {
            msg.result = new pt_base_categoryb().GetEntity(d => d.ID == ID);
        }


        //删除分类
        public void Cate_Del(ref Msg_Result msg, int Client, int ID)
        {
            //var tt = new PT_Base_CategoryB().GetEntity(p => p.ID == ID);
            //if (tt.Client == 0)
            //{
            //    msg.error = "系统分类不能删除";
            //}
            //else
            //{
            //有子分类不能删除
            if (new pt_base_categoryb().GetEntities(p => p.TopID == ID).Count() > 0)
            {
                msg.error = "有子分类不能删除";
            }
            else
            {
                if (Client == 0)
                {
                    new pt_base_categoryb().Delete(d => d.ID == ID);
                }
                else
                {
                    new pt_base_categoryb().Delete(d => d.Client == Client && d.ID == ID);
                }
            }

        }

        /// <summary>
        /// 设置分类显示或隐藏
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="Client"></param>
        /// <param name="ID"></param>
        public void Cate_Hide(ref Msg_Result msg, int Client, int ID)
        {
            string SQL = "update PT_Base_Category set ISShow=CASE ISShow WHEN 1 THEN 0 ELSE 1 END where ID=" + ID;
            new pt_base_categoryb().ExsSql(SQL);

        }

        #endregion

        #region 获取档案信息
        public void Sender_List(ref Msg_Result msg, int Client, Msg_Page page,string info, string infotype)
        {
            //string strwhere = "STYPE='" + infotype + "'";

            StringBuilder strsql = new StringBuilder();
            strsql.AppendFormat("select * from SENDERINFO where 1=1 ");
            if (infotype != "")
            {
                strsql.AppendFormat(" and STYPE=N'{0}'", infotype);
            }
            if (info != "")
            {
                strsql.AppendFormat(" and (SNAME LIKE N'%{0}%' OR CREDITID  LIKE N'%{0}%' OR CARDID  LIKE N'%{0}%' OR CONTACT  LIKE N'%{0}%')", info);
            }

            int total = 0;
            var dt =new wl_senderinfob().GetDTByCommand(strsql.ToString());
            msg.result = dt;
            msg.total = dt.Rows.Count;

        }

        public void Logistic_List(ref Msg_Result msg, int Client, Msg_Page page, string info)
        {
            //string strwhere = "STYPE='" + infotype + "'";

            StringBuilder strsql = new StringBuilder();
            strsql.AppendFormat("SELECT DISTINCT COMNAME,COMADDRESS,CREDITID,TRANSLICENSE,TRANSPIC,CONTACT,PHONE FROM CARINFO where 1=1 ");
            
            if (info != "")
            {
                strsql.AppendFormat(" and (COMNAME LIKE N'%{0}%' OR COMADDRESS  LIKE N'%{0}%' OR CREDITID  LIKE N'%{0}%' OR TRANSLICENSE  LIKE N'%{0}%')", info);
            }

            int total = 0;
            var dt = new wl_carinfob().GetDTByCommand(strsql.ToString());
            msg.result = dt;
            msg.total = dt.Rows.Count;

        }

        public void car_List(ref Msg_Result msg, int Client, Msg_Page page, string info)
        {
            //string strwhere = "STYPE='" + infotype + "'";

            StringBuilder strsql = new StringBuilder();
            strsql.AppendFormat("  SELECT DISTINCT COMNAME,CARID,CARCOLOR,SUPPLYER,ONTIME,CARMODEL,WEIGHT,CARTYPE,CHECKYEAR,RUNCHECK,CARSIZE,LINKCOMANY,OWNERPHONE,INSURANCE,INSURCOMPANY,INSURAMOUNT,INSURPICTURE,DRIVERNAME,DRIVERID,DRIVERPHONE FROM CARINFO where 1=1 ");

            if (info != "")
            {
                strsql.AppendFormat(" and (COMNAME LIKE N'%{0}%' OR CARID  LIKE N'%{0}%' OR CARCOLOR  LIKE N'%{0}%' OR CARMODEL  LIKE N'%{0}%')", info);
            }

            int total = 0;
            var dt = new wl_carinfob().GetDTByCommand(strsql.ToString());
            msg.result = dt;
            msg.total = dt.Rows.Count;

        }

        public void driver_List(ref Msg_Result msg, int Client, Msg_Page page, string info)
        {
            //string strwhere = "STYPE='" + infotype + "'";

            StringBuilder strsql = new StringBuilder();
            strsql.AppendFormat("  SELECT distinct DRIVERNAME,DRIVERID,DRIVERADD,DRIVERPHONE,ALLOWCAR,DRIVERTIME FROM CARINFO where 1=1 ");

            if (info != "")
            {
                strsql.AppendFormat(" and (DRIVERNAME LIKE N'%{0}%' OR DRIVERID  LIKE N'%{0}%' OR DRIVERADD  LIKE N'%{0}%' OR DRIVERPHONE  LIKE N'%{0}%')", info);
            }

            int total = 0;
            var dt = new wl_carinfob().GetDTByCommand(strsql.ToString());
            msg.result = dt;
            msg.total = dt.Rows.Count;

        }

        public void receiver_List(ref Msg_Result msg, int Client, Msg_Page page, string info)
        {
            //string strwhere = "STYPE='" + infotype + "'";

            StringBuilder strsql = new StringBuilder();
            strsql.AppendFormat("  select RECEIVER,RECEIVEPHONE,RECEIVERID,DOWNADD,DOWNFLOOR,PUTDOWMODE,PUTDOWNFEE from orderinfo where 1=1 ");

            if (info != "")
            {
                strsql.AppendFormat(" and (RECEIVER LIKE N'%{0}%' OR RECEIVEPHONE  LIKE N'%{0}%' OR RECEIVERID  LIKE N'%{0}%' OR DOWNADD  LIKE N'%{0}%')", info);
            }

            int total = 0;
            var dt = new wl_senderinfob().GetDTByCommand(strsql.ToString());
            msg.result = dt;
            msg.total = dt.Rows.Count;

        }

        public void order_List(ref Msg_Result msg, int Client, Msg_Page page, string info)
        {
            //string strwhere = "STYPE='" + infotype + "'";

            StringBuilder strsql = new StringBuilder();
            strsql.AppendFormat(" select ORDERID,PUTONADD, RECEIVER,RECEIVEPHONE,RECEIVERID,DOWNADD,DOWNFLOOR,PUTDOWMODE,PUTDOWNFEE,CARID from orderinfo where 1=1 ");

            if (info != "")
            {
                strsql.AppendFormat(" and (ORDERID LIKE N'%{0}%' OR DOWNADD  LIKE N'%{0}%' OR RECEIVER  LIKE N'%{0}%' OR RECEIVEPHONE  LIKE N'%{0}%')", info);
            }

            int total = 0;
            var dt = new wl_orderinfob().GetDTByCommand(strsql.ToString());
            msg.result = dt;
            msg.total = dt.Rows.Count;

        }
        public void orderitem_List(ref Msg_Result msg, int Client, Msg_Page page, string info)
        {
            //string strwhere = "STYPE='" + infotype + "'";

            StringBuilder strsql = new StringBuilder();
            strsql.AppendFormat(" SELECT ORDERID,ITEM,GOODS,QTY,VOLUME,WEIGHT,PACKAGE,CRETIME FROM ORDERITEM where ORDERID='{0}' ",info);


            int total = 0;
            var dt = new wl_orderitemb().GetDTByCommand(strsql.ToString());
            msg.result = dt;
            msg.total = dt.Rows.Count;

        }

        public void distince_List(ref Msg_Result msg, int Client, Msg_Page page, string info)
        {
            //string strwhere = "STYPE='" + infotype + "'";

            StringBuilder strsql = new StringBuilder();
            strsql.AppendFormat(" select DISTINCT ORDERID,SENDER,RECEIVER,PUTONADD,DOWNADD,DISTINCE,TRANSFEE from ORDERINFO where 1=1 ");

            if (info != "")
            {
                strsql.AppendFormat(" and (ORDERID LIKE N'%{0}%' OR SENDER  LIKE N'%{0}%' OR RECEIVER  LIKE N'%{0}%' OR PUTONADD  LIKE N'%{0}%'  OR DOWNADD  LIKE N'%{0}%')", info);
            }

            int total = 0;
            var dt = new wl_senderinfob().GetDTByCommand(strsql.ToString());
            msg.result = dt;
            msg.total = dt.Rows.Count;

        }

        public void Transfer_List(ref Msg_Result msg, int Client, Msg_Page page, string info)
        {
            //string strwhere = "STYPE='" + infotype + "'";

            StringBuilder strsql = new StringBuilder();
            strsql.AppendFormat(" SELECT DISTINCT PUTONADD,CARID FROM ORDERINFO WHERE STATUS='T' ");

            int total = 0;
            var dt = new wl_orderinfob().GetDTByCommand(strsql.ToString());
            msg.result = dt;
            msg.total = dt.Rows.Count;

        }

        public void Transfer_List1(ref Msg_Result msg, int Client, Msg_Page page, string info)
        {
            //string strwhere = "STYPE='" + infotype + "'";

            StringBuilder strsql = new StringBuilder();
            strsql.AppendFormat(" SELECT DISTINCT PUTONADD FROM ORDERINFO WHERE STATUS='T' ");

            int total = 0;
            var dt = new wl_orderinfob().GetDTByCommand(strsql.ToString());
            msg.result = dt;
            msg.total = dt.Rows.Count;

        }

        public void Update_car(ref Msg_Result msg, int Client, Msg_Page page, string carid,string orderid)
        {
            //string strwhere = "STYPE='" + infotype + "'";

            StringBuilder strsql = new StringBuilder();
            strsql.AppendFormat(" update [ORDERINFO] set CARID=N'{0}', STATUS='T' where ORDERID='{1}' ", carid, orderid);

            int total = 0;
            new wl_orderinfob().ExsSql(strsql.ToString());
            DataTable dt = new wl_orderinfob().GetDTByCommand("SELECT DISTINCT CARID, PUTONADD FROM ORDERINFO WHERE STATUS='T' AND CARID<>'' AND ORDERID='" + orderid + "'");

            
            msg.result = dt;
            msg.total = dt.Rows.Count;



        }
        #endregion


    }
}
